package com.alita.ramdom;

import com.alita.utils.MStrUtil;

/**
 * @Description: 电话号码
 * @Author: Luancx
 * @Date: 2022/02/21
 * @Version: 1.0.0
 */
public class PhoneRandom {
    private PhoneRandom() {
        throw new IllegalStateException("PhoneRandom class");
    }

    /**
     * 手机号前缀
     */
    private static final String[] TEL_FIRST = "134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153"
            .split(",");

    /**
     * 随机生成手机号码
     *
     * @return 手机号
     */
    protected static String randomPhone() {
        String prefix = TEL_FIRST[NumberRandom.randomInt(TEL_FIRST.length)];
        return randomPhone(prefix);
    }

    /**
     * 根据前缀随机生成手机号码
     *
     * @return 手机号
     */
    protected static String randomPhone(String prefix) {
        if (prefix.length() > 11) {
            return prefix.substring(0, 11);
        }
        if (MStrUtil.isBlank(prefix)) {
            prefix = TEL_FIRST[NumberRandom.randomInt(TEL_FIRST.length)];
        }
        int count = 11 - prefix.length();
        StringBuilder prefixBuilder = new StringBuilder(prefix);
        for (int i = 0; i < count; i++) {
            prefixBuilder.append(NumberRandom.randomInt(10));
        }
        prefix = String.valueOf(prefixBuilder);
        if (!prefix.startsWith("1")) {
            return "1" + prefix.substring(1);
        }
        return prefix;
    }
}
